package com.cloud.school.bus.teacherhelper.modules.settings;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.slidingfragment.BaseAboveFragment;
import com.cloud.school.bus.teacherhelper.base.utils.AppUserId;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.modules.SlidingActivity;
import com.cloud.school.bus.teacherhelper.modules.login.LoginActivity;
import com.cloud.school.bus.teacherhelper.modules.uploadlist.uploadutils.UploadFileUtils;
import com.cloud.school.bus.teacherhelper.protocol.ProtocolDef;

/**
 * 设置界面
 * 
 * @author lenovo
 * 
 */
public class SettingsFragment extends BaseAboveFragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.fragment_settings, null);
		setViewData(view);
		return view;
	}

	@Override
	protected void setViewData(View view) {

		setListener(view);
		setTitleNavBar(view);
	}

	@Override
	protected void setListener(View view) {
		ViewGroup modifyPasswordLayout = (ViewGroup) view
				.findViewById(R.id.modifyPasswordLayout);
		modifyPasswordLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mParentContext,
						ModifyPasswordActivity.class);
				startActivityForResult(intent, 0);
			}
		});
		ViewGroup aboutLayout = (ViewGroup) view.findViewById(R.id.aboutLayout);
		aboutLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mParentContext, AboutActivity.class);
				startActivity(intent);
			}
		});
		ViewGroup helpLayout = (ViewGroup) view.findViewById(R.id.helpLayout);
		helpLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(mParentContext, HelpActivity.class);
				intent.putExtra("url", ProtocolDef.URL_HELP);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void setTitleNavBar(View view) {
		TitleNavBarView titleNavBarView = (TitleNavBarView) view
				.findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.settings));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				((SlidingActivity) mParentContext).toggle();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 修改密码返回
			switch (resultCode) {
			case Activity.RESULT_OK: {
				AppUserId.setPassword(mParentContext, "");
				Intent intent = new Intent(mParentContext, LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				((Activity) mParentContext).finish();
				break;
			}
			case Activity.RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}
	
	/**
	 * 判断网络连接监听
	 */
	private BroadcastReceiver mNetConnectBroadcastReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
				UploadFileUtils.getUploadUtils().setContext(mParentContext);
				UploadFileUtils.getUploadUtils().setFragment(mFragment);
				UploadFileUtils.getUploadUtils().uploadFileService();
			}
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		// 注册广播
		IntentFilter netConnectIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
		mParentContext.registerReceiver(mNetConnectBroadcastReceiver, netConnectIntentFilter);
	}

	@Override
	public void onPause() {
		super.onPause();
		mParentContext.unregisterReceiver(mNetConnectBroadcastReceiver);
	}

}
